function getXPath(element) {
  if (!element || element.nodeType !== 1) return '';
  if (element.id) return `//*[@id="${element.id}"]`;

  let segments = [];
  let current = element;
  while (current && current.nodeType === 1) {
    let index = 1;
    let sibling = current.previousElementSibling;
    while (sibling) {
      if (sibling.tagName === current.tagName) {
        index++;
      }
      sibling = sibling.previousElementSibling;
    }
    segments.unshift(`${current.tagName.toLowerCase()}[${index}]`);
    if (current.parentNode && current.parentNode.nodeType === 1 && current.parentNode.id) {
      segments.unshift(`*[@id="${current.parentNode.id}"]`);
      break;
    }
    current = current.parentNode;
  }
  return '//' + segments.join('/');
}


// function getXPath(element) {
//   if (!element || element.nodeType !== 1) return '';
//   if (element.id) return `//*[@id="${element.id}"]`;

//   let xpath = '';
//   let current = element;
//   while (current && current.nodeType === 1) {
//     let index = 1;
//     let sibling = current.previousElementSibling;
//     while (sibling) {
//       if (sibling.tagName === current.tagName) {
//         index++;
//       }
//       sibling = sibling.previousElementSibling;
//     }
//     xpath = `/${current.tagName.toLowerCase()}[${index}]` + xpath;
//     current = current.parentNode;
//     if (current && current.nodeType === 1 && current.id) {
//       xpath = `//*[@id="${current.id}"]` + xpath;
//       break;
//     }
//   }
//   return xpath;
// }